<?php

/**
 * 默认控制器
 */
class Controller_Default extends Controller_Abstract
{
	/**********/
	function actionUserwel(){
		$show_box['title'] = '按日期查询';
		$this->_view['form'] = new Form_OpSearch(url('default::default/serverlog'));
		$this->_view['form']->_subject = '按日期查询';
		//$this->_view['title'] = '首页-当前状态';
		$this->_view['nowuser']= $_SESSION['acl_task_userdata']['account'];
		$this->_view['nowgroup']= $_SESSION['acl_task_userdata']['group_name'];
		//dump($_SESSION);
	}

	function actionTests(){
		$table =QDB::getConn();
		$sql = "SELECT `sys_acl_permissions`.*   FROM `log`.`sys_acl_permissions`  INNER JOIN `log`.`sys_acl_roles_have_permissions`  `m`   ON (`m`.`role_id` = '6')   WHERE ((`sys_acl_permissions`.`permission_id` = `m`.`permission_id`))";
		$handel =$table->execute($sql);
		$temp = $handel->fetchAll();
		$allhave = array();
		foreach ($temp as $s){
			$allhave[]= $s['aliasname'];
		}
		dump($allhave);
	}

	function actionUserwelcome(){
		
		//$this->_view['form'] = new Form_OpSearch(url('default::default/serverlog'));
		//$this->_view['form']->_subject = '查询系统日志';
		$show_box['title'] = '当前状态';
		$this->_view['title'] = '当前状态';
		$this->_view['nowuser']= $_SESSION['acl_task_userdata']['account'];
		$this->_view['nowgroup']= $_SESSION['acl_task_userdata']['group_name'];
		//dump($_SESSION);
	}

		function actionToplog(){
	
		$demo = array();
		// 设置页面列表数目
		$pagesize = 10;
		// 获取当前页数，通常为get方式参数
		//$_REQUEST['start']
		$page = ($_REQUEST['start']/$_REQUEST['limit'])+1;
		$this->_view['pageno']=$page;
		//$select = AlarmTable::find()->limitPage($_REQUEST['start'], $_REQUEST['limit']);
		$select = AlarmTable::find()->limitPage($page,$_REQUEST['limit']);
		$allc=$select->getPagination();
		$totaldata = $allc['record_count'];
		$demostr=$select->order("id desc")->getAll()->toArray();
		$date = date("Y-m-d");
		for($i=0; $i<count($demostr); $i++){
			$demo[] = array("date"=>$demostr[$i]['id'],"pageview"=>$demostr[$i]['date'],"ip"=>$demostr[$i]['macaddress'],"visits"=>$demostr[$i]['time'],"uv"=>$demostr[$i]['security'],"pvuv"=>$demostr[$i]['infotype'],"op"=>"删除");
			$date = date("Y-m-d",strtotime($date)-24*3600);
			
		}
		

		//$demo_out = array_slice($demo,$_REQUEST['start'],$_REQUEST['limit']);
		$str = '({"total":"'.$totaldata.'","root":'.json_encode($demo).'})';
		//dump($demostr);
		echo $str;
	}

	function actionToptenlog(){
		$show_box['title'] = '最新报警记录';
		$this->_view['title'] = '最新报警记录';
		$addconds='';$addconds1='';
		$this->_view['startd']='';
		$this->_view['endd']='';
		$this->_view['countno'] = 1;$this->_view['countno1'] = 1;
		if ($this->_context->isPOST())
		{
				if($_POST['startdate']==$_POST['enddate']){
					$endd=$this->my_date_add('d',$_POST['enddate'],1);
				}else{
					$endd=$_POST['enddate'];
				}
				$this->_view['startd']=$_POST['startdate'];
				$this->_view['endd']=$endd;
				$addconds="time >='".$_POST['startdate']."' and time<='".$endd."'";
				$addconds1="date >='".$_POST['startdate']."' and date<='".$endd."'";
				//echo $this->my_date_add('d',$_POST['enddate'],1);
				//echo $addconds;
				//dump($_POST);
		}

		if($this->_context->startd !='' && $this->_context->endd !=''){
			$this->_view['startd']=$this->_context->startd;
			$this->_view['endd']=$this->_context->endd;
			$addconds="time >='".$this->_context->startd."' and time<='".$this->_context->endd."'";
			$addconds1="date >='".$this->_context->startd."' and date<='".$this->_context->endd."'";
		}
		
		$select = EventAlarm::find($addconds)->limit(0, 10);
		$this->_view['group'] = $select->order("id desc")->getAll();

		$select1 = AlarmTable::find($addconds1)->limit(0, 10);
		$this->_view['group1'] = $select1->order("id desc")->getAll();
		
	}

	/****接收邮箱******/
	function actionRemailing(){
		
		$this->_view['form'] = new Form_TheMail(url('default::default/remailing'));
		
		//添加
		if($this->_context->isPOST()){
			try{
				if($_POST['hid']==''){
					// 查询指定用户 ID
					$edit_one = TheMail::find('mail = ?', $_POST['mail'])->query();
					if (!$edit_one->id())
					{	
						$newgroup = new TheMail($_POST);
						//print_r($form->values());
						$newgroup->save();
						// 如果用户 ID 无效，则返回用户管理首页
						//return $this->_redirect(url('default::rulesetting/index'));
					}else{
						return $this->_redirect(url('default::default/remailing'));
	//					$edit_one->changeProps($_POST);
	//					// 保存并重定向浏览器
	//					$edit_one->save();
	//					//return $this->_redirect(url('admin::users/index'));
	//					return $this->_redirectMessage(
	//							'修改配置成功', 
	//							'配置资料已经成功修改！',
	//							url('default::default/framework', array('control' => 'default','acts' => 'remailing|userwel')));
						
						//dump('ass');
					}
				}else{
					$edit_one = TheMail::find('id = ?', $_POST['hid'])->query();
					$edit_one->changeProps($_POST);
					// 保存并重定向浏览器
					$edit_one->save();
					//dump('ass');
				}

			}catch(QValidation_FailedException $ex){
				$form->invalidate($ex);
				return $this->_redirect(url('default::default/remailing'));
			}
		}else{
			//dump($this->_context->id);
			if(isset($this->_context->id)){
				list($id, $go) = explode('|', $this->_context->id);
				//dump($go);
				if($go=='edit'){
					$edit_one = TheMail::find('id = ?',$id)->query();
					if (!$edit_one->id())
					{
						return $this->_redirect(url('default::default/remailing'));
					}else{
						//dump($edit_one);
						$this->_view['form']->import($edit_one);
						$this->_view['form']['hid']->set('value',$id);
						//dump('开始修改s');
					}
				}else if($go=='del'){
					$rid = $id;
					TheMail::meta()->destroyWhere('id = ?', $rid);
					if(mysql_affected_rows())
					{
//						return $this->_redirectMessage(
//						'删除成功', 
//						'指定设置删除成功',
//						url('default::default/framework', array('control' => 'default','acts' => 'remailing|userwel')));
					}
					else
					{
						return $this->_redirectMessage(
						'删除失败', 
						'指定设置删除失败',
						url('default::default/framework', array('control' => 'default','acts' => 'remailing|userwel')));
					}
				}
			}
		}

		$show_box['title'] = '连接状态';
		$this->_view['title'] = '连接状态';
		$addconds='';
		$this->_view['startd']='';
		$this->_view['endd']='';
		$this->_view['countno'] = 1;

		// 设置页面列表数目
		$pagesize = 10;
		// 获取当前页数，通常为get方式参数
		$page = $this->_context->page;
		$this->_view['pageno']=$page;
		$select = TheMail::find()->limitPage($page, $pagesize);
		$this->_view['pager'] = $select->getPagination();
		$this->_view['group'] = $select->getAll();
		//echo $this->_view['pager']['record_count'];
		if($this->_view['pager']['record_count']>0 && $page=='')
			$page=1;

		$pagediv = new Helper_JsPager($this->_view['pager']['record_count'], $this->_view['pager']['page_size'], 9, $page, 'opgopage(\'default|remailing|','\')',5);
		$this->_view['pagestr']= $pagediv->show();
	}

	/****授权信息******/
	function actionLicense(){
		$this->_view['countno'] = 1;
		$select = ServerLicense::find()->limit(0, 1);
		$this->_view['group'] = $select->getAll();
		//dump($_SESSION);
	}

	/****服务器邮件通知设置******/
	function actionMailsetting(){
		$show_box['title'] = '服务器邮件通知设置';
        
		// 查询指定用户 ID
		$edit_one = ServerConfig::find('id=1')->query();
		//dump($edit_one);
		if ( !$edit_one->id() )
		{ // 如果用户 ID 无效，则返回用户管理首页
			return $this->_redirect(url('default::default/mailsetting'));
		}
		// 构造表单对象
        $form = new Form_MailSetting(url('default/mailsetting'));
        if ($this->_context->isPOST() && $form->validate($_POST))
        {
            $edit_one->changeProps($form->values());
			// 保存并重定向浏览器
			$edit_one->save();
				//return $this->_redirect(url('admin::users/index'));
				return $this->_redirectMessage(
						'修改配置成功', 
						'！',
						url('default::default/framework', array('control' => 'default','acts' => 'mailsetting|userwel')));
        }elseif (!$this->_context->isPOST())
		{
				// 如果不是 POST 提交，则把对象值导入表单
				//$edit_one['password'] = '';
				//$edit_one['rulename']=iconv("GB2312","UTF-8",$edit_one['rulename']);
				//dump($edit_one['serverconfig']);
				$form->import($edit_one);
		}
        // 将表单对象传递给视图
        $this->_view['form'] = $form;
        $this->_viewname = 'mailsetting';
	}

	/**********/
	function actionFramework(){
		$show_box['title'] = '框架页面';
		
		if(isset($this->_context->space)){
			$this->_view['space'] = 'namespace='.$this->_context->space.'&';

			if(isset($this->_context->control)){
				$this->_view['control'] = 'controller='.$this->_context->control.'&';
				
				if(isset($this->_context->acts)){
					$at=split('[|]',$this->_context->acts);
					if(count($at)>1){
						$this->_view['acts'] = 'action='.$at[0].'';
						$this->_view['acts1'] ='action='.$at[1].'';
					}else{
						$this->_view['acts'] = 'action='.$this->_context->acts.'';
						$this->_view['acts1'] ='action=secpart';
					}
				
				}else{
					$this->_view['acts'] ='';
					$this->_view['acts1'] ='action=secpart';
				}
				
			}else
				$this->_view['control'] ='';
		}
		else{
			$this->_view['space']='';
			
			if(isset($this->_context->control)){
				$this->_view['control'] = 'controller='.$this->_context->control.'&';
				
				if(isset($this->_context->acts)){
					
					$at=split('[|]',$this->_context->acts);
					
					if(count($at)>1){
						$this->_view['acts'] = 'action='.$at[0].'';
						$this->_view['acts1'] ='action='.$at[1].'';
					}else{
						$this->_view['acts'] = 'action='.$this->_context->acts.'';
						$this->_view['acts1'] ='action=secpart';
					}
					
					//$this->_view['acts'] = 'action='.$this->_context->acts.'';
					//$this->_view['acts1'] ='action=secpart';
				
				}else{
					$this->_view['acts'] ='';
					$this->_view['acts1'] ='action=secpart';
				}
				
			}else
				$this->_view['control'] ='';
		}
		if(isset($this->_context->user_id))
			$this->_view['userid'] = '&user_id='.$this->_context->user_id;
		else
			$this->_view['userid'] = '';

		if(isset($this->_context->mac))
			$this->_view['userid'] .= '&mac='.$this->_context->mac;

		if(isset($this->_context->group))
			$this->_view['userid'] .= '&groupid='.$this->_context->group;

		if(isset($this->_context->groupid))
			$this->_view['userid'] .= '&groupid='.$this->_context->groupid;

		if(isset($this->_context->id))
			$this->_view['userid'] .= '&id='.$this->_context->id;
	}

	/**********/
	function actionServerlog(){
		$show_box['title'] = '系统日志';
		$this->_view['title'] = '首页-系统日志';
		$addconds='';
		$this->_view['startd']='';
		$this->_view['endd']='';

		//search
		$this->_view['form'] = new Form_OpSearch(url('default::default/serverlog'));
		$this->_view['form']->_subject = '按日期查询';
		//$this->_view['title'] = '首页-当前状态';
		$this->_view['nowuser']= $_SESSION['acl_task_userdata']['account'];
		$this->_view['nowgroup']= $_SESSION['acl_task_userdata']['group_name'];

		if ($this->_context->isPOST())
		{
				if($_POST['startdate']==$_POST['enddate']){
					$endd=$this->my_date_add('d',$_POST['enddate'],1);
				}else{
					$endd=$_POST['enddate'];
				}
				$this->_view['startd']=$_POST['startdate'];
				$this->_view['endd']=$endd;
				$addconds="datetime >='".$_POST['startdate']."' and datetime<='".$endd."'";
				//echo $this->my_date_add('d',$_POST['enddate'],1);
				//echo $addconds;
				//dump($_POST);
		}

		if($this->_context->startd !='' && $this->_context->endd !=''){
				$this->_view['startd']=$this->_context->startd;
				$this->_view['endd']=$this->_context->endd;
				$addconds="datetime >='".$this->_context->startd."' and datetime<='".$this->_context->endd."'";
		}
		
		// 设置页面列表数目
		$pagesize = 10;
		// 获取当前页数，通常为get方式参数
		$page = $this->_context->page;
		$this->_view['pageno']=$page;
		$select = Serverlog::find($addconds)->limitPage($page, $pagesize);
		$this->_view['pager'] = $select->getPagination();
		$this->_view['group'] = $select->order("id desc")->getAll();
		//echo $this->_view['pager']['record_count'];
		if($this->_view['pager']['record_count']>0 && $page=='')
			$page=1;

		$pagediv = new Helper_JsPager($this->_view['pager']['record_count'], $this->_view['pager']['page_size'], 9, $page, 'opgopage(\'default|serverlog|','\')',5);
		$this->_view['pagestr']= $pagediv->show();

		
	}

	/**********/
	function actionEventalarm(){
		$show_box['title'] = '系统日志报警';
		$this->_view['title'] = '首页-系统日志报警';
		$addconds='';
		$this->_view['startd']='';
		$this->_view['endd']='';

		//search
		$this->_view['form'] = new Form_OpSearch(url('default::default/eventalarm'));
		$this->_view['form']->_subject = '按日期查询';
		//$this->_view['title'] = '首页-当前状态';
		$this->_view['nowuser']= $_SESSION['acl_task_userdata']['account'];
		$this->_view['nowgroup']= $_SESSION['acl_task_userdata']['group_name'];

		if ($this->_context->isPOST())
		{
				if($_POST['startdate']==$_POST['enddate']){
					$endd=$this->my_date_add('d',$_POST['enddate'],1);
				}else{
					$endd=$_POST['enddate'];
				}
				$this->_view['startd']=$_POST['startdate'];
				$this->_view['endd']=$endd;
				$addconds="time >='".$_POST['startdate']."' and time<='".$endd."'";
				//echo $this->my_date_add('d',$_POST['enddate'],1);
				//echo $addconds;
				//dump($_POST);
		}

		if(($this->_context->startd !='undefined' && $this->_context->endd !='undefined') && ($this->_context->startd !='' && $this->_context->endd !='')){
				$this->_view['startd']=$this->_context->startd;
				$this->_view['endd']=$this->_context->endd;
				$addconds="time >='".$this->_context->startd."' and time<='".$this->_context->endd."'";
		}
		
		// 设置页面列表数目
		$pagesize = 10;
		// 获取当前页数，通常为get方式参数
		$page = $this->_context->page;
		$this->_view['pageno']=$page;
		$select = EventAlarm::find($addconds)->limitPage($page, $pagesize);
		$this->_view['pager'] = $select->getPagination();
		$this->_view['group'] = $select->order("id desc")->getAll();
		$this->_view['countno'] = $this->_view['pager']['page_size']*$page+1;
		//echo $this->_view['pager']['record_count'];
		if($this->_view['pager']['record_count']>0 && $page=='')
			$page=1;

		$pagediv = new Helper_JsPager($this->_view['pager']['record_count'], $this->_view['pager']['page_size'], 9, $page, 'opgopage(\'default|eventalarm|','\')',5);
		$this->_view['pagestr']= $pagediv->show();

		
	}

	/**********/
	function actionAlarmtable(){
		$show_box['title'] = '行为日志报警';
		$this->_view['title'] = '首页-行为日志报警';
		$addconds='';
		$this->_view['startd']='';
		$this->_view['endd']='';

		//search
		$this->_view['form'] = new Form_OpSearch(url('default::default/alarmtable'));
		$this->_view['form']->_subject = '按日期查询';
		//$this->_view['title'] = '首页-当前状态';
		$this->_view['nowuser']= $_SESSION['acl_task_userdata']['account'];
		$this->_view['nowgroup']= $_SESSION['acl_task_userdata']['group_name'];

		if ($this->_context->isPOST())
		{
				if($_POST['startdate']==$_POST['enddate']){
					$endd=$this->my_date_add('d',$_POST['enddate'],1);
				}else{
					$endd=$_POST['enddate'];
				}
				$this->_view['startd']=$_POST['startdate'];
				$this->_view['endd']=$endd;
				$addconds="date >='".$_POST['startdate']."' and date<='".$endd."'";
				//echo $this->my_date_add('d',$_POST['enddate'],1);
				//echo $addconds;
				//dump($_POST);
		}

		if(($this->_context->startd !='undefined' && $this->_context->endd !='undefined') && ($this->_context->startd !='' && $this->_context->endd !='')){
				$this->_view['startd']=$this->_context->startd;
				$this->_view['endd']=$this->_context->endd;
				$addconds="date >='".$this->_context->startd."' and date<='".$this->_context->endd."'";
		}
		
		// 设置页面列表数目
		$pagesize = 10;
		// 获取当前页数，通常为get方式参数
		$page = $this->_context->page;
		$this->_view['pageno']=$page;
		$select = AlarmTable::find($addconds)->limitPage($page, $pagesize);
		$this->_view['pager'] = $select->getPagination();
		$this->_view['group'] = $select->order("id desc")->getAll();
		$this->_view['countno'] = $this->_view['pager']['page_size']*$page+1;
		//echo $this->_view['pager']['record_count'];
		if($this->_view['pager']['record_count']>0 && $page=='')
			$page=1;

		$pagediv = new Helper_JsPager($this->_view['pager']['record_count'], $this->_view['pager']['page_size'], 9, $page, 'opgopage(\'default|alarmtable|','\')',5);
		$this->_view['pagestr']= $pagediv->show();

		
	}

	/**********/
	function actionSystemInfo(){
		
		$show_box['title'] = '连接状态';
		$this->_view['title'] = '连接状态';
		$addconds='';
		$this->_view['startd']='';
		$this->_view['endd']='';
		$this->_view['countno'] = 1;
		
		//search
		//$this->_view['form'] = new Form_OpSearch(url('default::default/systeminfo'));
		//$this->_view['form']->_subject = '按日期查询';
		//$this->_view['title'] = '首页-当前状态';
		$this->_view['nowuser']= $_SESSION['acl_task_userdata']['account'];
		$this->_view['nowgroup']= $_SESSION['acl_task_userdata']['group_name'];
		//INTERVAL 30 SECOND改为INTERVAL 30 SECOND
		$addconds='(systeminfo.`lasttime` >= DATE_sub( NOW( ) , INTERVAL 30 SECOND) AND systeminfo.`lasttime` < DATE_ADD( NOW( ) , INTERVAL 30 SECOND))';
		
		// 设置页面列表数目
		$pagesize = 10;
		// 获取当前页数，通常为get方式参数
		$page = $this->_context->page;
		$this->_view['pageno']=$page;
		$select = SystemInfo::find($addconds)->limitPage($page, $pagesize);
		$this->_view['pager'] = $select->getPagination();
		$this->_view['group'] = $select->order("macaddress desc")->getAll();
		//echo $this->_view['pager']['record_count'];
		if($this->_view['pager']['record_count']>0 && $page=='')
			$page=1;

		$pagediv = new Helper_JsPager($this->_view['pager']['record_count'], $this->_view['pager']['page_size'], 9, $page, 'opgopage(\'default|systemInfo|','\')',5);
		$this->_view['pagestr']= $pagediv->show();
	}

    /* ******************************************************************** */
    function actionIndex()
    {
        $show_box['title'] = '系统信息';
        if ($this->_app->currentUserRoles())
        {
            // 如果已经登录，就转到任务列表页面
            $show_box['ncArrs'] = Helper_Permissions::getNamespaceControllerArrs();
            $this->_view['show_box'] = $show_box;
        }
        else
        {
            // 未登录则转到登录页面
            return $this->_redirect(url('default/login'));
        }
    }

	function actionWelcome()
    {
        $show_box['title'] = '欢迎页面';
        if ($this->_app->currentUserRoles())
        {
            // 如果已经登录，就转到任务列表页面
            $this->_view['hello']= '欢迎登录后台！';
            $this->_view['show_box'] = $show_box;
        }
        else
        {
            // 未登录则转到登录页面
            return $this->_redirect(url('default/login'));
        }
    }

	/* ******************************************************************** */
    function actionIndexpart()
    {
        $show_box['title'] = '管理首页';
        if ($this->_app->currentUserRoles())
        {
            // 如果已经登录，就转到任务列表页面
            $show_box['ncArrs'] = Helper_Permissions::getNamespaceControllerArrs();
            $this->_view['show_box'] = $show_box;
        }
        else
        {
            // 未登录则转到登录页面
            return $this->_redirect(url('default/login'));
        }
    }

	function actionSecpart()
    {
        $show_box['title'] = '默认首页';
        if ($this->_app->currentUserRoles())
        {
            // 如果已经登录，就转到任务列表页面
            //$show_box['ncArrs'] = Helper_Permissions::getNamespaceControllerArrs();
            //$this->_view['show_box'] = $show_box;
        }
        else
        {
            // 未登录则转到登录页面
            return $this->_redirect(url('default/login'));
        }
    }

	function actionImgcode() {
		$pagediv = new Helper_ImCode();
		$this->_view['stxt']= $pagediv->image();
	}
    /* ******************************************************************** */
    function actionLogin()
    {
        //echo 'df';
		$this->_view['title'] = '登录系统';
		$this->_view['form'] = new Form_UserLogin(url('default/login'));
		//$ati= new Helper_ImgCode();
		//$this->_view['stxt']=$ati->clear(); 
		$imgcode = new Helper_ImCode();
		//$this->_view['stxt']= $pagediv->image();
		//        $this->_view['form'] = new Form_UserLogin(url('default/login'));
		//        $this->_view['form']->_subject = $show_box['title'];
        if ($this->_context->isPOST() && $this->_view['form']->validate($_POST))
        {
            try
            {
                if($imgcode->check($_POST['codes'])){
				
				// 使用 acluser 插件的 validateLogin() 方法验证登录并取得有效的 user 对象
                $user = Admin_Users::meta()->validateLogin($this->_view['form']['account']->value, $this->_view['form']['password']->value);
                $userInfo = $user->aclData();
                if ( $userInfo['is_locked'] ){ 
                //判断用户是否被锁定
                    $this->_view['form']['account']->invalidate("被锁定的用户，请与管理员联系。");
                }else{
                    // 获取用户权限组，并将登录用户的信息存入 SESSION，以便应用程序记住用户的登录状态
                        //用扩展的函数获取用户权限，直接判定用户的权限，并根据用户的状态进行角色转换
                    $Helper_Acl = new Helper_Acl;
                    $user_AllRoles = $Helper_Acl->UserAclRoles($userInfo['user_id']);
                    $this->_app->changeCurrentUser($user->aclData(), $user_AllRoles);
                    // 登录成功后，重定向浏览器
//                    return $this->_redirectMessage(
//                        '登录成功', 
//                        '您已经成功登录。登录后可以创建和修改任务列表。',
//                        url('default/index'));
					return $this->_redirect(url('default/index'));
                }

				}else{
					$this->_view['form']['password']->invalidate("验证码不正确");
				}
            }
            catch (AclUser_UsernameNotFoundException $ex)
            {
                //$this->_view['form']['account']->invalidate("您输入的用户名 {$this->_view['form']['account']->value} 不存在");
				$this->_view['form']['account']->invalidate("用户不存在或者密码错误");
				if(!$imgcode->check($_POST['codes']))
					$this->_view['form']['password']->invalidate("验证码不正确");
            }
            catch (AclUser_WrongPasswordException $ex)
            {
                $this->_view['form']['password']->invalidate("用户不存在或者密码错误");
            }
        }
        //$this->_view['show_box'] = $show_box;
        //$this->_viewname = 'index';
    }

    /* ******************************************************************** */
    function actionLogout()
    {
        $show_box['title'] = '注销系统';
        $this->_app->cleanCurrentUser();
        //return $this->_redirectMessage(
        //    '退出登录', 
        //    '您已经退出了登录状态，谢谢使用。',
        //    url('default/index'));
		return $this->_redirect(url('default/index'));
		
    }

    /* ******************************************************************** */
    function actionRegister()
    {
        $show_box['title'] = '会员注册';
        // 构造表单对象
        $form = new Form_UserReg(url('default/register'));
        if ($this->_context->isPOST() && $form->validate($_POST))
        {
            // 是 POST 提交，并且表单验证通过
            try
            {
                // 创建 User 对象并保存
                $user = new Admin_Users($form->values());
                // 自动添加用户的默认组
                $user->roles[] = Admin_Roles::find('rolename = ?', 'UNCHECKED')->query();
                $user->save();
                // 成功后重定向到登录页面
                return $this->_redirectMessage(
                    '注册新用户', 
                    '您的新帐户已经注册成功，请输入用户名和密码登录。',
                    url('default/login'));
            }
            catch (AclUser_DuplicateUsernameException $ex)
            {
                // 捕获 AclUser_DuplicateUsernameException 异常，在表单中指出用户名存在重复问题
                $form['account']->invalidate("您要注册的帐号 {$user->account} 已经存在了");
            }
        }
        // 将表单对象传递给视图
        $this->_view['form'] = $form;
        $this->_viewname = 'form';
    }

    /* ******************************************************************** */
    function actionChangePasswd()
    {
        $show_box['title'] = '修改密码';
        $form = new Form_ChangePasswd(url('default/changepasswd'));

        if ($this->_context->isPOST() && $form->validate($_POST))
        {
            try
            {
                // 取得当前用户的信息
                $current_user = $this->_app->currentUser();
                // 修改密码
                Admin_Users::meta()->changePassword($current_user['account'], $form['new_password']->value, $form['old_password']->value);
                // 重定向浏览器
                return $this->_redirectMessage(
                    '修改密码成功', 
                    '您的登录密码已经成功修改，下次登录请使用新密码。',
                    url('default/index'));
            }
            catch (AclUser_WrongPasswordException $ex)
            {
                $form['old_password']->invalidate('您输入的现用密码不正确');
            }
        }

        $this->_view['form'] = $form;
        $this->_viewname = 'pwdform';
    }

    function my_date_add($opt,$time,$num){
		$year = date('Y',strtotime($time));
		$month = date('m',strtotime($time));
		$day = date('d',strtotime($time));
		$last_day = date('t',strtotime($time));
		if($opt=='y'){
			$year = $year + $num;
		}else if($opt=='d'){
			$day = $day + $num;
		}else if($opt=='m'){
			$month = $month + $num;
			if($day == $last_day){//如果是最后一天的话
				$day = date('t',strtotime("{$year}-{$month}-01"));
			}
		} else {
			return $time;
		}
		return date("Y-m-d", mktime(0, 0, 0, $month, $day, $year));
	}
}